home *** CD-ROM | disk | FTP | other *** search
- Path: wilks.demon.co.uk!Ian
- From: Ian M Wilks <Ian@wilks.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Need a linked list program
- Date: Fri, 29 Mar 1996 23:52:46 +0000
- Organization: Ian
- Distribution: world
- Message-ID: <5N$xDCAOfHXxEwSh@wilks.demon.co.uk>
- References: <4jf76k$avb@zeus.tcp.co.uk>
- NNTP-Posting-Host: wilks.demon.co.uk
- X-NNTP-Posting-Host: wilks.demon.co.uk
- MIME-Version: 1.0
- X-Newsreader: Turnpike Version 1.10 <xKZkCHGGMISoFYLnpOdd648JTZ>
-
- In article <4jf76k$avb@zeus.tcp.co.uk>, David <daveg@tcp.co.uk> writes
- >Does anyone out there have a linked list program which moves in both
- >directions and can also create, delete and search items in the list?
- >
- >I'm doing a study into data structures at the moment and this would
- >really help.
- >
-
- Hope this helps. Unfortunately, its in C++ but should point you in the
- right direction:
-
- #include <fstream.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <conio.h>
- #define YES 1
- #define NO 0
-
- struct address
- {
- char name[40];
- char street[40];
- char town[30];
- char county[30];
- char postcode[10];
- address * next; //pointer to next entry
- address * previous; //pointer to previous entry
- };
-
- address *start, *last;
- fstream list_file;
-
- address * find(char *);
- void enter(), search(), save(), load(), list();
- void delete_entry(address **, address **);
- void store(address *, address **, address **);
- void display(address *);
- int select_menu();
-
- void main()
- {
- int another_go = YES;
- start = last = NULL; //initialize first and last pointers
-
- while(another_go == YES)
- {
- switch(select_menu())
- {
- case 1:
- enter(); //enter data into list
- break;
- case 2:
- delete_entry(&start, &last);
- break;
- case 3:
- list(); //list the data
- break;
- case 4:
- search(); //find a record
- break;
- case 5:
- save(); //save list to disc file
- break;
- case 6:
- load(); //load list from disc file
- break;
- case 7:
- another_go = NO;
- break;
- default:
- cout << "\n\nInternal Error\n\n";
- } //end of switch statement
- } //end of while loop
- } //end of main()
-
- int select_menu()
- {
- int item;
- clrscr();
- cout << endl << "\t 1\tMake Entry\n" << "\t 2\tDelete Entry\n"
- << "\t 3\tList Entries\n" << "\t 4\tSearch\n"
- << "\t 5\tSave to File\n" << "\t 6\tLoad from File\n"
- << "\t 7\tExit from Program\n\n";
- do
- {
- cout << "\t\t\tEnter Choice: ";
- cin >> item;
- }while(item < 0 || item > 7);
- return(item);
- }
-
- //Enter data into list
- void enter()
- {
- address * info;
- char again = 'Y';
- do
- {
- if(!(info = new address))
- {//check if memory allocated - if not terminate program
- cout << "\n\nMEMORY ALLOCATION ERROR";
- exit(-1);
- }
- clrscr();
- cout << "\n\tEnter Name: ";
- gets(info->name);
- cout << "\n\tEnter Street: ";
- gets(info->street);
- cout << "\n\tEnter Town: ";
- gets(info->town);
- cout << "\n\tEnter County: ";
- gets(info->county);
- cout << "\n\tEnter Postcode: ";
- gets(info->postcode);
-
- store(info, &start, &last); //put data into list
-
- cout << "\n\n\nEnter another? (Y/N) ";
- cin >> again;
- }while( again == 'Y' || again == 'y');
- }
-
- //create linked list
- void store(address * i, address ** start, address **last)
- {
- if (*start == NULL) //first record has to be treated
- { //specially
- i->next = NULL; //if first record - no next
- //record
- i->previous = NULL; //if first record - no previous
- //record
- *last = i; //store details in last
- *start = i; //store details in start
- }
- else //not first record
- {
- (*last)->next = i;
- i->next = NULL;
- i->previous = *last;
- *last = i;
- }
- }
-
- //remove entry from list
- void delete_entry(address ** start, address ** last)
- {
- address * info;
- char name[40];
-
- clrscr();
- cout << "\n\n\t\tEnter Name: ";
- gets(name);
- info = find(name); //find record
- if(info)
- {
- if(*start == info) //if start of list is to be
- //deleted
- { //new start of list is needed
- *start = info->next;
- if(*start)
- (*start)->previous = NULL;
- else
- *last = NULL;
- }
- else
- {
- info->previous->next = info->next;
- if(info != *last)
- info->next->previous = info->previous;
- else
- *last = info->previous;
- }
- delete info;
- }
- else
- gotoxy(25, 25);
- cout << "Press a key to continue.";
- getch();
- }
-
- //find address
- address * find(char * name)
- {
- address * info;
-
- info = start;
-
- while(info)
- {
- if(!strcmp(name, info->name))
- return(info);
- info = info->next;
- }
- cout << "\n\n\tName not found\n";
- return(NULL);
- }
-
- //display list
- void list()
- {
- address * info;
- int count = 0;
-
- clrscr();
- info = start;
-
- while(info)
- {
- count++;
- display(info);
- info = info->next;
-
- if ( (count % 4) == 0) //to prevent records scrolling
- //off screen
- {
- gotoxy(25,25);
- cout << "Press a key to continue";
- getch();
- clrscr();
- }
- }
- cout << endl << endl;
- gotoxy(25, 25);
- cout << "Press a key to continue.";
- getch();
- }
-
- //print list to screen
- void display(address *info)
- {
- cout << endl << "\t" << info->name << endl
- << "\t" << info->street << endl
- << "\t" << info->town << endl
- << "\t" << info->county
- << ", " << info->postcode << endl;
- }
-
- //look for name in list
- void search()
- {
- char name[40];
- address * info;
-
- clrscr();
- cout << "\n\nEnter name to find: ";
- gets(name);
-
- info = find(name);
-
- if (info)
- {
- display(info);
- gotoxy(25, 25);
- cout << "Press a key to continue.";
- getch();
- }
- else
- {
- gotoxy(25, 25);
- cout << "Press a key to continue.";
- getch();
- }
- }
-
-
- //save list to disc
- void save()
- {
- address * info;
-
- rename("list.dat", "list.old"); //create backup file
- list_file.open("list.dat",ios::out);
- if(!list_file)
- {
- cout << "\n\nCould not open file.";
- exit(-1);
- }
- cout << "\n\n\t\tSAVING FILE\n";
-
- info = start;
- while(info)
- {
- list_file << info->name << endl << info->street << endl
- << info->town << endl << info->county << endl
- << info->postcode << endl;
- info = info->next;
- }
- list_file.close();
- }
-
- //load from disc
- void load()
- {
- address * info;
-
- list_file.open("list.dat", ios::in);
- if(!list_file)
- {
- cout << "\n\nCould not open file.";
- exit(-1);
- }
-
- while(start) //delete any lists already in memory
- {
- info = start->next;
- delete info;
- start = info;
- }
- start = last = NULL;
-
- cout << "\n\n\t\tLOADING FILE\n";
-
- while(!list_file.eof())
- {
- info = new address;
- if(!info)
- {
- cout << "\n\n\t\tMEMORY ALLOCATION ERROR\n";
- exit(-1);
- }
- list_file.getline(info->name, 40);
- list_file.getline(info->street, 40);
- list_file.getline(info->town, 30);
- list_file.getline(info->county, 30);
- list_file.getline(info->postcode, 10);
-
- if(!( list_file.eof()))
- {
- store(info, &start, &last);
- }
- }
- list_file.close();
- }
-
-
-
- // If you want to store the list in ascending order use this function
- //instead:
- /*
- void store(address * i, address ** start, address ** last)
- {
- address * old, * point;
-
- if(*last == NULL) //first element in list
- {
- i->next = NULL;
- i->previous = NULL;
- *last = i;
- *start = i;
- return;
- }
-
- point = *start; //start at top of list
-
- old = NULL;
-
- while(point)
- {
- if(strcmp(point->name, i->name) < 0)
- {
- old = point;
- point = point->next;
- }
- else
- {
- if(point->previous)
- {
- point->previous->next = i;
- i->next = point;
- i->previous = point->previous;
- point->previous = i;
- return;
- }
- i->next = point;
- i->previous = NULL;
- point->previous = i;
- *start = i;
- return;
- }
- }
- old->next = i;
- i->next = NULL;
- i->previous = old;
- *last = i;
- }
- */
-